home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Info-Mac 3
/
Info_Mac_1994-01.iso
/
Development
/
General
/
Appsprites
/
APPMAKER CODE
/
FileM.c
< prev
next >
Wrap
Text File
|
1993-07-11
|
6KB
|
316 lines
/* FileM.c */
/* Created 7/11/93 11:43 AM by AppMaker */
#include <Types.h>
#include <Quickdraw.h>
#include <Controls.h>
#include <Dialogs.h>
#include <Events.h>
#include <Lists.h>
#include <Menus.h>
#include <TextEdit.h>
#include <Desk.h>
#include <Packages.h>
#include <ToolUtils.h>
#include "ResourceDefs.h"
#include "Dispatcher.h"
#include "Miscellany.h"
#include "Globals.h"
#include "AppspritesData.h"
#include "FileM.h"
#define dialogTop 75
#define dialogLeft 85
short numOpenTypes;
SFTypeList openTypeList;
static void CloseAppWindow (void);
static void DoNew (void);
static void DoOpen (void);
static void DoSave (void);
static void DoSaveAs (void);
static void DoRevert (void);
static void DoPageSetup (void);
static void DoPrint (void);
/*----------*/
void InitFileM (void)
{
numOpenTypes = 1;
openTypeList [0] = kFileType;
} /*InitFileM*/
/*----------*/
Boolean OkToOpen (OSType fType)
{
short i;
enum {searching, found, notFound}
status;
i = 0;
status = searching;
while (status == searching) {
if (i >= numOpenTypes) {
status = notFound;
} else {
if (fType == openTypeList [i]) {
status = found;
} else {
i++;
}
}
} /*while*/
return (status == found);
} /*OkToOpen*/
/*----------*/
static void DoNew (void)
{
OpenWindows ("\p", 0, 0);
InitAppData ();
} /*DoNew*/
/*----------*/
void OpenDoc (Str255 fileName,
short vRefNum)
{
/* This *should* be defined in an Apple interface file: */
#define StationeryFlag 0x0800
Boolean isStationery;
FInfo finderInfo;
short fRefNum;
isStationery = false;
if (GetFInfo (fileName, vRefNum, &finderInfo) == noErr) {
if ((finderInfo.fdFlags & StationeryFlag) != 0) {
isStationery = true;
}
}
if (OpenAppFile (vRefNum, fileName, &fRefNum)) {
if (isStationery) {
OpenWindows ("\p", 0, 0);
ReadAppFile (fRefNum);
CloseAppFile (fRefNum);
} else {
OpenWindows (fileName, vRefNum, fRefNum);
ReadAppFile (fRefNum);
}
}
} /*OpenDoc*/
/*----------*/
static void DoOpen (void)
{
Point dialogOrigin;
SFReply sfInfo;
SetPt (&dialogOrigin, dialogLeft, dialogTop);
SFGetFile (dialogOrigin, "\p", nil, numOpenTypes, openTypeList, nil, &sfInfo);
if (sfInfo.good) {
OpenDoc (sfInfo.fName, sfInfo.vRefNum);
}
} /*DoOpen*/
/*----------*/
void OpenApp (void)
{
DoNew ();
} /*OpenApp*/
/*----------*/
static void DoSaveAs (void)
{
SFReply sfInfo;
short fRefNum;
StringHandle prompt;
Str255 promptStr;
Str255 suggestion;
prompt = GetString (SaveAsPromptID);
if (prompt != nil) {
BlockMove (&(**prompt), promptStr, GetHandleSize ((Handle) prompt));
} else {
promptStr [0] = 0;
}
BlockMove (&(**cur->filename), suggestion, GetHandleSize ((Handle) cur->filename));
if (CreateFile (&sfInfo, promptStr, suggestion, kSignature, kFileType)) {
CloseAppFile (cur->fileNum);
if (OpenAppFile (sfInfo.vRefNum, sfInfo.fName, &fRefNum)) {
SetWTitle (curWindow, sfInfo.fName);
cur->fileNum = fRefNum;
cur->volNum = sfInfo.vRefNum;
SetString (cur->filename, sfInfo.fName);
WriteAppFile (cur->fileNum);
cur->dirty = false;
} else { /*should never happen*/
SetWTitle (curWindow, "\p???");
cur->fileNum = 0;
cur->volNum = 0;
}
}
} /*DoSaveAs*/
/*----------*/
static void DoSave (void)
{
if (cur->fileNum == 0) {
DoSaveAs ();
} else {
WriteAppFile (cur->fileNum);
cur->dirty = false;
}
} /*DoSave*/
/*----------*/
static void CloseAppWindow (void)
{
enum {saveItem = 1, cancelItem, discardItem};
Str255 curTitle;
short itemNum;
Boolean okay;
okay = true;
SetInfo (FrontWindow ());
if (cur->dirty) {
GetWTitle (curWindow, curTitle);
ParamText (curTitle, "\p", "\p", "\p");
InitCursor ();
itemNum = Alert (SaveID, nil);
switch (itemNum) {
case saveItem:
DoSave ();
okay = !errorFlag;
break;
case discardItem:
/*Do nothing*/;
break;
case cancelItem:
errorFlag = true;
okay = false;
break;
} /*switch*/
}
if (okay) {
DisposeAppData ();
if (cur->windowKind == 1) { /* 1st or only window in set */
CloseAppFile (cur->fileNum);
}
CloseCurWindow ();
}
} /*CloseAppWindow*/
/*----------*/
void DoClose (void)
{
WindowPeek frontPeek;
errorFlag = false;
frontPeek = (WindowPeek) FrontWindow ();
if (frontPeek->windowKind < 0) {
CloseDeskAcc (frontPeek->windowKind);
} else if (frontPeek->windowKind == dialogKind) {
CloseModelessDialog (FrontWindow ());
} else {
CloseAppWindow ();
}
} /*DoClose*/
/*----------*/
void DoQuit (void)
{
Boolean quitting;
quitting = true;
while (quitting && (FrontWindow () != nil)) {
SystemTask ();
DoClose ();
if (errorFlag) {
quitting = false;
}
} /*while*/
if (quitting) {
quittingTime = true;
}
} /*DoQuit*/
/*----------*/
static void DoRevert ()
{
Str255 fileName;
Boolean okay;
okay = true;
if (cur->dirty) {
GetWTitle (curWindow, fileName);
ParamText (fileName, "\p", "\p", "\p");
okay = Confirm (RevertID);
}
if (okay) {
cur->dirty = false;
DisposeAppData ();
if (cur->fileNum != 0) {
ReadAppFile (cur->fileNum);
}
InvalRect (&curWindow->portRect);
}
} /*DoRevert*/
/*----------*/
static void DoPageSetup (void)
{
} /*DoPageSetup*/
/*----------*/
static void DoPrint (void)
{
} /*DoPrint*/
/*----------*/
void DoFile (short itemNr)
{
errorFlag = false;
switch (itemNr) {
case FileNew:
DoNew ();
break;
case FileOpen:
DoOpen ();
break;
case FileClose:
DoClose ();
break;
case FileSave:
DoSave ();
break;
case FileSaveAs:
DoSaveAs ();
break;
case FileRevert:
DoRevert ();
break;
case FilePageSetup:
DoPageSetup ();
break;
case FilePrint:
DoPrint ();
break;
case FileQuit:
DoQuit ();
break;
} /*switch*/
} /*DoFile*/
/* File */